package test2;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * 先继承类，后继承接口
 * @author fengke
 */
public class DrawListener extends MouseAdapter implements ActionListener{

    //s 初始点；e 最终点
    private int x1, y1, x2, y2,x3,y3,sx,sy,ex,ey;
    private Graphics2D g2;
    private String str = "Line";
    private Color color;
    //用于标记是否是第一次画线
    private int flag = 1;

    public void setG(Graphics g){
        g2 = (Graphics2D) g;
        // 设置画笔开启抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

    }

    @Override
    public void actionPerformed(ActionEvent e){
        //getSource返回事件源类型，也可以用getActionCommand返回String类型
        JButton button = (JButton) e.getSource();

        //如果为图形按钮
        if(button.getText() != "")
        {
            str = button.getText();
        }
        else{
            color = button.getBackground();
            System.out.println("color: "+color);
        }
    }

    /**
     * 矩形、填充矩形、填充圆角矩形、椭圆、填充椭圆、填充扇形暂时还不能自定义大小
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e){
        //按下和释放要在同一位置才是点击
        System.out.println("点击");
        x2 = e.getX();
        y2 = e.getY();

        //画空心矩形
        if(str.equals("Rect")){
            //构造方法传参数
            Shape s = new Shape(x2,y2,80,40,color,
                    new BasicStroke(1),"Rect");
            s.draw(g2);
        }
        //画填充矩形
        if(str.equals("fillRect")){
            Shape s = new Shape(x2,y2,80,40,color,
                    new BasicStroke(1),"fillRect");
            s.draw(g2);
        }
        //画圆角矩形 还有bug
        if(str.equals("RoundRect")){
            Shape s = new Shape(x2,y2,80,40,color,
                    new BasicStroke(1),"fillRect");
            s.draw(g2);
        }
        //画空心圆
        if(str.equals("Oval")){
            Shape s = new Shape(x2,y2,80,40,color,
                    new BasicStroke(1),"Oval");
            s.draw(g2);
        }
        //画填充圆
        if(str.equals("fillOval")){
            Shape s = new Shape(x2,y2,80,40,color,
                    new BasicStroke(1),"fillOval");
            s.draw(g2);
        }
        //画填充弧
        if(str.equals("fillArc")){
            Shape s = new Shape(x2,y2,80,80,color,
                    new BasicStroke(1),"fillArc");
            s.draw(g2);
        }
        //画任意多边形
        if(str.equals("Polygon")){
            if(flag == 2){
                Shape s = new Shape(ex,ey,x2,y2,color,
                        new BasicStroke(1),"Line");
                s.draw(g2);
                ex = x2;
                ey = y2;
            }
            //如果与起点较近或鼠标双击，则闭合多边形
            if((Math.abs(sx-x2)<10 && Math.abs(sy-y2)<10)
                    ||(e.getClickCount()==2)){
                Shape s = new Shape(sx,sy,x2,y2,color,
                        new BasicStroke(1),"Line");
                s.draw(g2);
                flag = 1;
            }
        }
    }

    //点鼠标
    @Override
    public void mousePressed(MouseEvent e){
        x1 = e.getX();
        y1 = e.getY();
        g2.setColor(color);
        if(flag == 1){
            sx = x1;
            sy = y1;
        }
    }

    //释放鼠标
    @Override
    public void mouseReleased(MouseEvent e){
        x2 = e.getX();
        y2 = e.getY();

        //画直线
        if(str.equals("Line")){
            Shape s = new Shape(x1,y1,x2,y2,color,
                    new BasicStroke(1),"Line");
            s.draw(g2);
        }
        //画任意多边形
        if(str.equals("Polygon")){
            if(flag == 1){
                Shape s = new Shape(x1,y1,x2,y2,color,
                        new BasicStroke(1),"Line");
                s.draw(g2);
                ex = x2;
                ey = y2;
                flag = 2;
            }
        }
        //画等腰三角形
        if(str.equals("Triangle")){
            Shape s = new Shape(x1,y1,x2,y2,color,
                    new BasicStroke(1),"Triangle");
            s.draw(g2);
        }
    }

}
